package com.offcn.webui.controller;

import com.offcn.common.response.AppResponse;
import com.offcn.webui.service.OrderFeignService;
import com.offcn.webui.vo.req.OrderFormInfoSubmitVo;
import com.offcn.webui.vo.resp.ReturnPayConfirmVo;
import com.offcn.webui.vo.resp.TOrder;
import com.offcn.webui.vo.resp.UserRespVo;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderFeignService orderService;

    @RequestMapping("/save")
    public String OrderSave(OrderFormInfoSubmitVo submitVo, Model model, HttpSession session){
        // 用户是否登录
        UserRespVo userVo = (UserRespVo)session.getAttribute("user");
        if(userVo == null){
            return "redirect:/login.html";
        }
        // 获取令牌
        String accessToken = userVo.getAccessToken();
        submitVo.setAccessToken(accessToken);
        ReturnPayConfirmVo returnVo = (ReturnPayConfirmVo)session.getAttribute("returnConfirm");
        // 添加项目Id
        submitVo.setProjectid(returnVo.getProjectId());
        // 添加项目说明
        submitVo.setRemark(returnVo.getProjectRemark());
        // 添加回报数量
        submitVo.setRtncount(returnVo.getNum());
        submitVo.setReturnid(returnVo.getId());

        // 提交订单
        AppResponse<TOrder> response = orderService.createOrder(submitVo);
        TOrder order = response.getData();

        // 输出内容
//        System.out.println(order.getReturnid());
//        System.out.println(order.getMoney());
//        System.out.println(order.getProjectid());

        System.out.println("order.getReturnid() = " + order.getReturnid());
        System.out.println("order.getMoney() = " + order.getMoney());
        System.out.println("order.getProjectid() = " + order.getProjectid());
        System.out.println("order.getAddress() = " + order.getAddress());
        System.out.println("order.getInvoice() = " + order.getInvoice());

        return "member/minecrowdfunding";

    }
}
